// @/views/Comment.vue
<template>
  <div class="comments">
    <myheader title="精彩评论">
      <span slot="left" class="iconfont iconjiantou2" @click="$router.back()"></span>
    </myheader>
    <div class="lists">
      <div class="item" v-for="(v,i) in commentList" :key='i'>
        <div class="head">
          <img :src="v.user.head_img" alt />
          <div>
            <p>{{v.user.nickname}}</p>
            <span>{{$moment(v.create_date).fromNow()}}</span>
          </div>
          <span @click="replayComment(v)">回复</span>
        </div>
        <commentItem v-if="v.parent" :parent="v.parent" @replayComment='replayComment'></commentItem>
        <div class="text">{{v.content}}</div>
      </div>
    </div>
    <commentFooter :post="article" @refresh="refresh" :obj='comment' @resetObj='comment = null'></commentFooter>
  </div>
</template>

<script>
import myheader from "@/components/myheader.vue";
import commentItem from '@/components/commentItem.vue'
import commentFooter from '@/components/commentFooter.vue'
import {getArticleCommentsList,getArticleDetailById} from '@/apis/article.js'
import axios from '@/utils/myaxios.js'
import {dateFormat} from '@/utils/myfilter.js'
export default {
  data () {
    return {
      commentList:[],
      article:{},
      comment:{}  //父组件中单击回复的时候，需要传递给子组件的评论数据对象
    }
  },
  components: {
    myheader,commentItem,commentFooter
  },
  filters: {
    dateFormat
  },
  methods: {
    // 单击回复
    replayComment(v){
      this.comment = v
    },
    // 列表数据的刷新
    refresh(){
      this.init()
      window.scrollTo(0,0)
    },
    async init(){
      let id = this.$route.params.id
      let res = await getArticleCommentsList(id)
      console.log(res);
      this.commentList = res.data.data.map(v=>{
        v.user.head_img = axios.defaults.baseURL + '/uploads/image/default.jpeg'
        return v
    })

    // 获取文章详情,这里重新的获取文章数据的原因是评论的数量已经修改了
    let result = await getArticleDetailById(id)
    this.article = result.data.data
    }
  },
  async mounted () {
    this.init()
  }
};
</script>

<style lang='less' scoped>
.lists {
  border-top: 5px solid #ddd;
  padding: 0 15px 50px;
  .item {
    padding: 10px 0;
    border-bottom: 1px solid #ccc;
    .head {
      display: flex;
      justify-content: space-between;
      align-items: center;
      > img {
        width: 50/360 * 100vw;
        height: 50/360 * 100vw;
        display: block;
        border-radius: 50%;
      }
      > div {
        flex: 1;
        display: flex;
        flex-direction: column;
        margin-left: 10px;
        > span {
          font-size: 12px;
          color: #999;
          line-height: 25px;
        }
      }
      > span {
        color: #999;
        font-size: 13px;
      }
    }
    .text {
      font-size: 14px;
      color: #333;
      padding: 20px 0 10px 0;
    }
  }
}
</style>